package com.example.stray_animal_management_system.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stray_animal_management_system.admin.mapper.SystemSettingMapper;
import com.example.stray_animal_management_system.admin.service.SystemSettingService;
import com.example.stray_animal_management_system.entity.SystemSetting;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/9
 */
/**
 * 系统设置服务实现类
 */
@Service
public class SystemSettingServiceImpl extends ServiceImpl<SystemSettingMapper, SystemSetting> implements SystemSettingService {

    @Override
    public List<SystemSetting> getAllSettings() {
        return list();
    }

    @Override
    public SystemSetting getByKey(String settingKey) {
        LambdaQueryWrapper<SystemSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemSetting::getSettingKey, settingKey);
        return getOne(wrapper);
    }

    @Override
    public boolean addSetting(SystemSetting setting) {
        // 检查键名是否已存在
        SystemSetting existing = getByKey(setting.getSettingKey());
        if (existing != null) {
            throw new RuntimeException("设置键名已存在");
        }
        return save(setting);
    }

    @Override
    public boolean updateSetting(SystemSetting setting) {
        // 如果更新了键名，检查新键名是否已存在
        if (setting.getSettingKey() != null) {
            SystemSetting existing = getByKey(setting.getSettingKey());
            if (existing != null && !existing.getId().equals(setting.getId())) {
                throw new RuntimeException("设置键名已存在");
            }
        }
        return updateById(setting);
    }

    @Override
    public boolean deleteSetting(Long id) {
        return removeById(id);
    }
}
